// mobile bind
// ============

define(['zepto', 'sui', 'dropload'], function($){
  return {
    init: function(data) {
      if(location.href.indexOf("#reloaded")==-1){
        location.href=location.href+"#reloaded";
        location.reload();
      }
      this.initScroll();
    },
    
    initScroll: function() {
        var page = 1;
        var size = 5;
        var haspage = $(".list-container").find(".item-content").length ? $(".list-container").find(".item-content").length  : false
        if(haspage >= 5) {
        $('.list-content').dropload({
            scrollArea : $(".profile-wallet-content"),
            autoLoad: false,
            domDown : {                                                          // 下方DOM
                domNoData  : '<div class="dropload-noData">暂无新数据</div>'
            },
            loadDownFn : function(me){
                page++;
                var result = '';
                $.ajax({
                    type: 'GET',
                    url: window.__CTX__.root + '/profile/transaction-records?page='+page+'&size='+size,
                    dataType: 'json',
                    success: function(data){
                        var transactionReocrd = data.data.transactionRecords
                        var arrLen = transactionReocrd.length;
                        if(arrLen > 0){
                            for(var i=0; i<arrLen; i++){
                                var now = transactionReocrd[i]
                                if (now.transaction_type === 'RECHARGE'){
                                    result += '<div class="item-content">'
                                        + '<div class="item-inner">'
                                        + '<div class="col-50 detail">'
                                        + ' <p class="no-margin">充值订单：' + now.recharge_record.code + '</p>'
                                        + '<p class="no-margin"> ' + now.recharge_record.create_time + '</p>'
                                        + '</div>'
                                        + '<div class="col-50 royalty">'
                                        + '<div class="color-green price">+ ' + now.recharge_record.amount / 100 + '</div>'
                                        + '</div>'
                                        + '</div>'
                                        + '</div>'
                                } else if(now.transaction_type === 'WITHDRAW'){
                                    result += '<div class="item-content">'
                                        + '<div class="item-inner">'
                                        + '<div class="col-50 detail">'
                                        + ' <p class="no-margin">提现订单： </p>'
                                        + '<p class="no-margin"> ' + now.withdraw_record.create_time + '</p>'
                                        + '</div>'
                                        + '<div class="col-50 royalty">'
                                        + '<div class="color-green price">+ ' + now.withdraw_record.amount / 100 + '</div>'
                                        + '</div>'
                                        + '</div>'
                                        + '</div>'
                                } else if(now.transaction_type === 'AGENT_DISTRIBUTION_REWARD'){
                                    result += '<div class="item-content">'
                                        + '<div class="item-inner">'
                                        + '<div class="col-50 detail">'
                                        + ' <p class="no-margin">代理提成： </p>'
                                        + '<p class="no-margin"> ' + now.distribution_reward_record.create_time + '</p>'
                                        + '</div>'
                                        + '<div class="col-50 royalty">'
                                        + '<div class="color-green price">+ ' + now.amount / 100 + '</div>'
                                        + '</div>'
                                        + '</div>'
                                        + '</div>'
                                } else if(now.transaction_type === 'RENT_CAR'){
                                    result += '<div class="item-content">'
                                        + '<div class="item-inner">'
                                        + '<div class="col-50 detail">'
                                        + ' <p class="no-margin">租车订单： ' + now.rent_car_record.order.code + '</p>'
                                        + '<p class="no-margin"> ' + now.rent_car_record.create_time + '</p>'
                                        + '</div>'
                                        + '<div class="col-50 royalty">'
                                        + '<div class="color-green price">+ ' + now.withdraw_record.amount / 100 + '</div>'
                                        + '</div>'
                                        + '</div>'
                                        + '</div>'
                                }
                            }
                        // 如果没有数据
                        }else{
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();
                        }
                        setTimeout(function(){
                            $('.list-container').append(result);
                            // 每次数据插入，必须重置
                            me.resetload();
                        },1000);
                    },
                    error: function(xhr, type){
                        alert('Ajax error!');
                        me.resetload();
                    }
                });
            }
        });
      } else {
    	$(".list-container").empty().append("<p class='no-margin text-center' style='padding: .5rem 0;'>无数据</p>")  
      }
    }
  }
})